package com.cbw.wx.controller;

import com.cbw.bean.BootStrapTableExpress;
import com.cbw.bean.Express;
import com.cbw.bean.Message;
import com.cbw.bean.User;
import com.cbw.mvc.ResponseBody;
import com.cbw.mvc.ResponseView;
import com.cbw.service.ExpressService;
import com.cbw.util.DateFormatUtil;
import com.cbw.util.JSONUtil;
import com.cbw.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class QRCodeController {
    /**
     * 生成二维码(初始化)
     * 根据type判断需要生成的二维码类型
     * type=user
     *      用户二维码 被扫后，获取手机号，快递员(柜子)端展示用户所有快递
     * type=express
     *      快递二维码 被扫后，获取取件码code,展示单个快递信息
     * @param request
     * @param response
     * @return
     */
    @ResponseView("/wx/createQRCode.do")
    public String createQrcode(HttpServletRequest request, HttpServletResponse response){

        //在微信端扫码有两种渠道：快递二维码|用户二维码：express|user
        //1.在主页index.html点击“收货二维码”功能进行扫码 window.location.href= "/wx/createQRCode.do?type=user";
        //2.在快递列表expressList.html中点击“二维码”超链接进行扫码 <a href="/wx/createQRCode.do?type=express&code="+data0[i].code">二维码</a>
        String type = request.getParameter("type");
        String userPhone=null;
        String qRCodeContent=null;//二维码封装的消息(code/userPhone)存入qRCodeContent,然后存入session
        //判断二维码类型
        if ("express".equals(type)){
            //快递二维码:被扫后，获取取件码code,展示单个快递信息
            //接收参数:取件码code
            String code = request.getParameter("code");
            //qRCodeContent="express_"+code;
            qRCodeContent=code;
        }else {
            //用户二维码:被扫后，获取手机号，快递员(柜子)端展示用户所有快递
            //接收参数:手机号
            User wxUser = UserUtil.getWxUser(request.getSession());
            userPhone = wxUser.getUserPhone();
            //qRCodeContent="userPhone_"+userPhone;
            qRCodeContent=userPhone;
        }
        //将qRCodeContent存入session
        HttpSession session = request.getSession();
        session.setAttribute("qrcode",qRCodeContent);

        return "/personQRcode.html";
    }

    /**
     * 扫码后获取二维码内容 qRCodeContent 即取件码或者手机号
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/qrcode.do")
    public String getQRCode(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();
        String qrcode = (String)session.getAttribute("qrcode");
        Message msg = new Message();
        //判断qrcode是否存在
        if (qrcode==null){
            msg.setStatus(-1);
            msg.setResult("取件码获取出错,请用户重新操作");
        }else {
            msg.setStatus(0);
            msg.setResult(qrcode);
        }
        System.out.println("二维码内容:"+qrcode);
        return JSONUtil.toJSON(msg);
    }

    /**
     * 扫码后，根据取件码取件，确认收货
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/updateStatus.do")
    public String updateStatus(HttpServletRequest request, HttpServletResponse response){
        //接收参数
        String code = request.getParameter("code");
        //service
        boolean flag = ExpressService.updateStatus(code);
        //填充消息对象
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("取件成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("取件码不存在，请用户更新二维码");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    /**
     * 根据取件码取件
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/findExpressByCode.do")
    public String findExpressByCode(HttpServletRequest request, HttpServletResponse response){
        //接收参数
        String code = request.getParameter("code");
        //查询快递
        Express e = ExpressService.findByCode(code);
        BootStrapTableExpress e1=null;
        if (e!=null){
            //语义化express
            e1=new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),
                    e.getUserPhone(),e.getCompany(),e.getCode(),
                    DateFormatUtil.format(e.getInTime()),
                    e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime()),
                    e.getStatus()==0?"待取件":"已取件",e.getSysPhone() );
        }
        Message msg = new Message();
        if (e1==null){
            msg.setStatus(-1);
            msg.setResult("取件码不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(e1);
        }
        return JSONUtil.toJSON(msg);
    }

}
